/**
 * 初始化整体流量图标
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/funnel');
    var zrnumber = require('echarts/util/number');

    var myChart1 = ec.init(document.getElementById('customerfunnel')); 

   var option = {
    title : {
        text: '客户余额漏斗图',
        subtext: '客户余额结构',
        x: 'left',
        y: 'bottom'
    },
    tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c}%"
    },
    toolbox: {
        show : true,
        orient: 'vertical',
        y: 'center',
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    legend: {
        orient: 'vertical',
        x: 'left',
        data :['有账户客户','余额>0客户','余额>=50客户','现金余额>0客户','现金余额>=50客户']
    },
    calculable : true,
    series : [
        {
            name:'非会员客户漏斗图',
            type:'funnel',
            width: '40%',
            height: '45%',
            x:'25%',
            y:'5%',
            data:[]
        },
        {
            name:'会员客户漏斗图',
            type:'funnel',
            width: '40%',
            height: '45%',
            x:'25%',
            y:'50%',
            sort : 'ascending',
            itemStyle: {
                normal: {
                    // color: 各异,
                    label: {
                        position: 'left'
                    }
                }
            },
            data:[]
        }
    ]
};
    var nameMap_0 = {"balance_0":"余额>0客户","balance_50":"余额>=50客户","cash_balance_0":"现金余额>0客户","cash_balance_50":"现金余额>=50客户","user_num":"有账户客户"};                
    function show(){
        var businessName=cate;
        var dateGrp = $('#customerFunnelOption').attr('dateGrp');
        var selDate=$('#time_input_FunnelPro').val();

        myChart1.showLoading({ text: '正在努力的读取数据中...' });
            $.ajax({
                    url: '/dashboard/income/ajax/customerFunnelData',
                    type: 'post',
                    async: true,
                    data:{"dateGroup":dateGrp,"businessName":businessName,"selectDate":selDate},
                    dataType: 'json',
                    success: function(data) {
                       var i=0;
                       option.series[0].data=[];
                       option.series[1].data=[];
                       if(selDate=='')
                       {
                         $('#time_input_FunnelPro').val(data['maxdate']);
                       }  
                       $.each(data['0'],function(name,colName){
                        option.series[0].data.push({
                                name:nameMap_0[name],
                                value:colName
                            });
                        });
                       i=0;
                       $.each(data['1'],function(name,colName){
                        option.series[1].data.push({
                                name:nameMap_0[name],
                                value:colName
                            });
                        });
                       myChart1.setOption(option,true);
                       myChart1.hideLoading();

                        },
                    error : function() {
                        myChart1.hideLoading();
                    }
                    });
    }
    var EC={
        show:show,
        resize:function(){myChart1.resize();}
    };
     function formatShowDate(yyyymmdd){
        var dateGrp = $('#customerProOption').attr('dateGrp');
        if('stat_date'==dateGrp){
            return yyyymmdd;
        }else if('week_begin'==dateGrp){
            return yyyymmdd;
        }else if('month_num'==dateGrp){
            return yyyymmdd.substr(0,4)+"年"+yyyymmdd.substr(4,2)+"月";
        }
    }

    function isDay(){
        var dateGrp = $('#customerProOption').attr('dateGrp');
        if('stat_date'==dateGrp){
            return true;
        }
        return false;
    }

        function isWeek(){
         var dateGrp = $('#customerProOption').attr('dateGrp');
        if('week_begin'==dateGrp){
            return true;
        }
        return false;
    }

    function isMonth(){
        var dateGrp = $('#customerProOption').attr('dateGrp');
        if('month_num'==dateGrp){
            return true;
        }
        return false;
    }
     //绑定书刷新按钮点击事件
    $("#refreshFunnel_btn").click(function(){  
        EC.show();
    });
    $(":button.btnCustomerfunnelPro[dateGrp]").click(function(){
        changeDateGrp(this);
        $('#time_checkbox_FunnelPro').attr('checked',false);
        $('#time_input_FunnelPro').val('');
        $('#time_input_FunnelPro').attr('disabled','');
        if($(this).attr('dateGrp')=='week_begin'){
            $('#time_input_FunnelPro').parent().hide();
        }else{
            $('#time_input_FunnelPro').parent().show()
        }
        
        EC.show();
    });
    //时间勾选框事件
    $('#time_checkbox_FunnelPro').change(function(e){
        var input=$('#time_input_FunnelPro');
        if(!input)return;
        if($(this).is(":checked")){
            input.removeAttr('disabled');
            input.attr('placeholder','选择时间'); 
            datepicker_op.format='yyyy-mm-dd';              
           /* EC.hideDataZoom();*/
           input.datepicker(datepicker_op);
           input.datepicker('setStartDate',new Date(getDate('2014/01/01')));
           var enddate=new Date();
           input.datepicker('setEndDate',new Date(getDate(enddate.toLocaleDateString())));
            /*setDatePicker(EC, $('#customerFunnelOption').attr('dateGrp'),input);*/
        }else{
            input.val('');
            input.attr('disabled','');
            input.attr('placeholder','选择时间');               
        }
    });
    /*//时间选择事件
    $('#time_input_FunnelPro').datepicker().on('changeDate',function(e){
        EC.addCompare_1(e.format());
        EC.addCompare_2(e.format());
    });*/

    return EC;
});

